#!/usr/bin/env bash
# Copyright 2017 Sourcerer, Inc. All Rights Reserved.
# License: MIT, https://github.com/sourcerer-io/sourcerer-app/blob/master/LICENSE.md
# Source code: https://github.com/sourcerer-io

echo "Installing sourcerer app.."
SERVER=$SERVER_EXT
DOWNLOAD_URL=$SERVER/app/download
SCRIPT_DIR=$HOME/.sourcerer
JAR_DIR=$SCRIPT_DIR

if [ -f $SCRIPT_DIR/sourcerer ] ; then
  read -p "Previous version of sourcerer is detected. Reinstall it? [Y/n] " yesno < /dev/tty
  if [ "$yesno" ] && [ "$yesno" = "n" ] ; then
    echo "Exiting"
    exit
  fi
fi

mkdir -p $SCRIPT_DIR
mkdir -p $JAR_DIR
curl -s $DOWNLOAD_URL > $JAR_DIR/sourcerer.jar

cat <<EOF > $SCRIPT_DIR/sourcerer
#!/usr/bin/env bash
# Copyright 2017 Sourcerer, Inc. All Rights Reserved.
# License: MIT, https://github.com/sourcerer-io/sourcerer-app/blob/master/LICENSE.md
# Source code: https://github.com/sourcerer-io

if [ "\$1" = "--uninstall" ] ; then
  read -p "The script will uninstall sourcerer app. Proceed? [Y/n] " yesno < /dev/tty
  if [ "$yesno" ] && [ "$yesno" = "n" ] ; then
    echo "Exiting"
    exit
  fi

  rm -f /usr/local/bin/sourcerer
  rm $SCRIPT_DIR/sourcerer
  rm -r $JAR_DIR

  echo "Done!"
  exit
fi

VERSION=0
if which java > /dev/null ; then
  VERSION_STR=\$(java -version 2>&1 | awk -F '"' '/version/ {print \$2}')
  MAJOR_VERSION=\$(echo \$VERSION_STR | cut -d. -f1)
  MINOR_VERSION=\$(echo \$VERSION_STR | cut -d. -f2)

  if [[ "\$MAJOR_VERSION" = "10" || "\$MAJOR_VERSION" > "10" ]]; then  # Format: 10
    VERSION=10
  elif [[ "\$MAJOR_VERSION" = "9" ]]; then  # Format: 9.0.1
    VERSION=9
  elif [[ "\$MAJOR_VERSION" = "1" && "\$MINOR_VERSION" = "8" ]]; then  # Format: 1.8.1
    VERSION=8
  else
    echo "Installed $VERSION_STR version of Java is not supported."
  fi
fi

if [ \$VERSION = 0 ] ; then
  if [ `uname` = "Darwin" ] ; then
    echo "Sourcerer requires JDK 8+ installed on the system. You can download it from:"
    echo "http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html"
  else
    echo "Sourcerer requires JRE 8+ installed on the system. You can download it from:"
    echo "http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html"
  fi
  exit 1
fi

# Java 9 and 10 requires additional parameters.
if [[ \$VERSION = 9 ]] ; then
  java --add-modules java.activation --add-opens java.base/java.nio=ALL-UNNAMED -jar $JAR_DIR/sourcerer.jar "\$@"
elif [[ \$VERSION = 10 ]] ; then
  java --add-opens java.base/java.nio=ALL-UNNAMED -jar $JAR_DIR/sourcerer.jar "\$@"
else
  java -jar $JAR_DIR/sourcerer.jar "\$@"
fi
EOF

chmod +x $SCRIPT_DIR/sourcerer

{
  rm -f /usr/local/bin/sourcerer 2> /dev/null &&
  ln -s $SCRIPT_DIR/sourcerer /usr/local/bin/sourcerer 2> /dev/null &&
  echo 'Done!' &&
  echo 'Run sourcerer to start hashing your repos!'
} || {
  echo 'We installed app to ~/.sourcerer/sourcerer.'
  echo 'You can add it to $PATH or run specified command to finish installation:'
  echo "sudo ln -s $SCRIPT_DIR/sourcerer /usr/local/bin/sourcerer"
  echo 'Then run sourcerer to start hashing your repos!'
}
